package org.web.ssm.controller;

import com.alibaba.fastjson.JSON;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.web.ssm.model.User;
import org.web.ssm.dto.ResultModel;
import org.web.ssm.service.IUserService;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by LUOLIANG on 2016/5/14.
 */
@Controller
@RequestMapping("/Userlogin")
@SessionAttributes("user")
public class LoginController {
    @Resource
    private IUserService userService;

    /**
     * 判断用户是否登陆成功，通过json返回结果
     *
     * @param name
     * @param pwd
     * @return json
     */
    @RequestMapping(value = "/isLogin", method = RequestMethod.POST)
    @ResponseBody
    public String isLogin(String name, String pwd, Model model, HttpSession httpSession) {
        ResultModel<User> resModel = new ResultModel<User>();
        Map<Object, String> user = new HashMap<Object, String>();

        if (name.equals("admin") && pwd.equals("admin")) {
            String URL = "manager/main.jsp";
            httpSession.setAttribute("URL", URL);
            resModel.setSuccess(true);
            resModel.setMsg("登录成功");
            user.put("adminManage", "user");
        } else {
            resModel.setSuccess(false);
            resModel.setMsg("登录失败");
        }

        System.out.println("返回json数据：" + JSON.toJSONString(resModel));
        //通过fastjson把resultmodel序列化成json数据
        return JSON.toJSONString(resModel);
    }


    @RequestMapping("/currentuser")
    @ResponseBody
    public String Currentuser(@ModelAttribute("user") User user) {
        return JSON.toJSONString(user);
    }
}
